#ifndef _PROCESSROUTER_TSPIDERSTATUS_H_
#define _PROCESSROUTER_TSPIDERSTATUS_H_

#include <list>
#include <string>
#include <iostream>
#include "message_struct.pb.h"
#include "rf/event/RFEventManager.h"
#include "rf/event/RFTimerFired.h"
#include "rf/event/rooftop_event.h"
#include <zookeeper/zookeeper.h>

namespace ProcessRouter {

class ManagerInfo {
    public:
    ManagerInfo();
    ~ManagerInfo();

    int initFromLocalFile(std::string path);

    ManagerConfiguer* configuer() { return m_infoStruct.mutable_configuer(); }

    std::string ip() { return m_ip; }
    std::string protocol() { return m_infoStruct.protocol(); }

    long freemem() { return m_infoStruct.freemem(); } 
    long usedmem() { return m_infoStruct.usedmem(); } 
    double mempercent() { return m_infoStruct.mempercent(); } 

    bool updateCorePages(Rooftop::EventBase &e);
    bool updateCoreStatus(Rooftop::EventBase &e);
    void removeCoreStatus(int cpid);
    void setCoreSize(int size);
    void setConnectSize(int size);

    int getFreePid(bool isStatic = false);

    std::string serialize();
    std::string serializePageInfo();

    bool inNeedDestroyProcess(int cpid);
    void removeFromNeedDestroyProcessList(int cpid);

    bool isEnableProxy() { return m_enableProxy; }
    const std::string & proxyType() { return m_proxyType; }
    const std::string & proxyAuthUser() { return m_proxyAuthUser; }
    const std::string & proxyAuthPass() { return m_proxyAuthPass; }
    const std::string & proxyHost() { return m_proxyHost; }
    const int proxyPort() { return m_proxyPort; }

    bool isEnableProxyPool() { return m_enableProxyPool; }
    std::string getProxyFromPool();
    void setProxyPool(std::list<std::string> proxys);

    private:

    void updateMemInfo();
    int parseConfiguer(std::string content);
    std::string getIPAddress();
    void connectProxyPool();

    std::string m_ip;
    std::list<int> m_needDestroyProcess;
    ManagerInfoStruct m_infoStruct;
    std::map<int, CorePages> m_pageInfoMap;
    boost::mutex m_mutex;

    bool m_enableProxy;
    std::string m_proxyType;
    std::string m_proxyAuthUser;
    std::string m_proxyAuthPass;
    std::string m_proxyHost;
    int m_proxyPort;

    bool m_enableProxyPool;
    std::string m_proxyPoolPath;
    std::list<std::string> m_proxyPoolList;
    int m_proxyPoolPos;
    zhandle_t* m_zkhandle;
};

}

#endif

